﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMP_QuanLySV.DAL;
using SMP_QuanLySV.BLL;

namespace SMP_QuanLySV.Presentation
{
    public partial class UC_CTDT : UserControl
    {

        Form parent;
        BLLChuongTrinhDaoTao BLL = new BLLChuongTrinhDaoTao();
        BLLNganh BLLNganh = new BLLNganh();
        BLLLop_SV BLLLop = new BLLLop_SV();
        

        int selectedIndex = -1;
        ChuongTrinhDaoTao selectedInstance = null;
        string trangThai = "";

        List<Lop_SV> listLop;
        List<NganhHoc> listNganh;

        // constructor
        public UC_CTDT(Form parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        void setDefaultSelectedInstance()
        {
            selectedIndex = 0;
            selectedInstance = BLL.retrieveByIndex(selectedIndex);
            // update du lieu cho phu hop voi selected row
            if (selectedInstance != null)
                updateTextboxes(selectedInstance);
        }

        // update cac textboxes cho phu hop voi selected row trong datagridview
        void updateTextboxes(ChuongTrinhDaoTao temp)
        {
            txtCTDTID.Text = temp.CTDT_ID;
            for (int i = 0; i < listLop.Count; i++)
                if (listLop[i].LopID == temp.LopID)
                {
                    cboLopID.SelectedIndex = i;
                    break;
                }
            for (int i = 0; i < listNganh.Count; i++)
                if (listNganh[i].NganhID == temp.NganhID)
                {
                    cboNganhID.SelectedIndex = i;
                    break;
                }
        }

        // set thuoc tinh readOnly/enabled cho tat ca cac controls ve 1 gia tri
        void setReadOnlyAll(bool val)
        {
            txtCTDTID.ReadOnly = val;
            cboLopID.Enabled = !val;
            cboNganhID.Enabled = !val;
        }

        // cho thuoc tinh text cua cac textboxes tro thanh ""
        private void clearTextboxes()
        {
            txtCTDTID.Text = "";
        }

        // xy ly su kien click chon 1 cell khac trong datagridview
        // --> lay row cua cell do lam selectedIndex va SelectedInstance bang row dc chon
        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedIndex = dataGridViewX1.SelectedCells[0].RowIndex;
            selectedInstance = BLL.retrieveByIndex(selectedIndex);
            updateTextboxes(selectedInstance);
        }

        void setBtnEnabled1(bool val)
        {
            btnThem.Enabled = val;
            btnXoa.Enabled = val;
            btnSua.Enabled = val;
        }

        void setBtnEnabled2(bool val)
        {
            btnLuu.Enabled = val;
            btnHuy.Enabled = val;
        }

        private void UC_CTDT_Load(object sender, EventArgs e)
        {
            // nap du lieu cho datagridview
            dataGridViewX1.DataSource = BLL.returnListCTDT();
            dataGridViewX1.Columns[3].Visible = false;
            dataGridViewX1.Columns[4].Visible = false;

            // nap du lieu cho combobox
            listNganh = BLLNganh.returnListNganh();
            cboNganhID.DataSource = listNganh;
            cboNganhID.DisplayMember = "TenNganh";
            cboNganhID.ValueMember = "NganhID";
            cboNganhID.SelectedIndex = 0;

            // nap du lieu cho combobox
            listLop = BLLLop.returnListLop();
            cboLopID.DataSource = listLop;
            cboLopID.DisplayMember = "TenLop";
            cboLopID.ValueMember = "LopID";
            cboLopID.SelectedIndex = 0;

            // mac dinh dong dau tien trong datagridview duoc chon (selected)
            setDefaultSelectedInstance();

            setReadOnlyAll(true);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            clearTextboxes();
            setReadOnlyAll(false);
            setBtnEnabled2(true);
            setBtnEnabled1(false);

            // set trang thai ve che do them moi
            trangThai = "dang_them";
            dataGridViewX1.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtCTDTID.Text == "")
            {
                MessageBox.Show("Bạn phải chọn CTDT nào cần xóa", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DialogResult result = MessageBox.Show("Bạn có chắc muốn xóa CTDT này không?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (result == DialogResult.Yes)
                {
                    // xoa doi tuong va update lai view
                    string CTDTIDCanDelete = txtCTDTID.Text;
                    ChuongTrinhDaoTao CTDTCanDelete = BLL.retrieveByID(CTDTIDCanDelete);
                    BLL.deleteCTDT(CTDTCanDelete);

                    dataGridViewX1.DataSource = BLL.returnListCTDT();
                    setDefaultSelectedInstance();
                }
            }

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            setReadOnlyAll(false);
            txtCTDTID.ReadOnly = true;

            // set trang thai ve che do sua
            trangThai = "dang_sua";
            setBtnEnabled1(false);
            setBtnEnabled2(true);
            dataGridViewX1.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            // disable toan bo cac textboxes
            setReadOnlyAll(true);

            // kiem tra tinh hop le cua du lieu nhap vao
            ChuongTrinhDaoTao tg = new ChuongTrinhDaoTao();
            tg.CTDT_ID = txtCTDTID.Text;
            tg.LopID = cboLopID.SelectedValue.ToString();
            tg.NganhID = cboNganhID.SelectedValue.ToString();

            if (BLL.kiemTra(tg))
            {
                // update csdl va view
                if (trangThai == "dang_sua")
                {
                    BLL.updateCTDT(tg);
                }
                if (trangThai == "dang_them")
                {
                    BLL.insertCTDT(tg);
                }
                dataGridViewX1.DataSource = BLL.returnListCTDT();

                dataGridViewX1.Enabled = true;
                // disable cac nut Luu va Huy
                setBtnEnabled2(false);
                setBtnEnabled1(true);
                setDefaultSelectedInstance();
            }
            else
            {
                MessageBox.Show("Bạn nhập sai thông tin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnHuy_Click(sender, e);
            }

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            // update lai view va buttons
            setDefaultSelectedInstance();
            setReadOnlyAll(true);
            dataGridViewX1.DataSource = BLL.returnListCTDT();
            setBtnEnabled2(false);
            setBtnEnabled1(true);

            dataGridViewX1.Enabled = true;
        }
    }
}
